package co.droidforum.foodx.activities.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import co.droidforum.foodx.R;
import co.droidforum.foodx.application.AplicationContext;
import co.droidforum.foodx.model.VenueDTO;
import co.droidforum.foodx.utils.droidImages.ImageLoader;

public class LugaresListAdapter extends BaseAdapter {
	private List<VenueDTO> datos;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	private Context context;
	
	

	public LugaresListAdapter(Context context, List<VenueDTO> objects) {
		this.context = context;
		datos = objects;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(AplicationContext.getContextApp());
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{		
		View item = convertView;
		if(convertView == null)
			item = inflater.inflate(R.layout.custom_item_list_lugares, null);
		final VenueDTO venueDTO = datos.get(position);
		TextView distancia = (TextView)item.findViewById(R.id.tv_distance);
		TextView nombre = (TextView)item.findViewById(R.id.tv_name);
		TextView categoria = (TextView)item.findViewById(R.id.tv_categoria);
		ImageView icoEstrellas = (ImageView)item.findViewById(R.id.tv_ico_estrellas);
		ImageView imagenIcono = (ImageView)item.findViewById(R.id.tv_image_ico);
		distancia.setText(Integer.toString(venueDTO.getDistance())+AplicationContext.getValueStringResource(R.string.unidad_medida_distance));
		nombre.setText(venueDTO.getName());
		categoria.setText(venueDTO.getCategoryShortName());
		icoEstrellas.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), this.getImagenEstrellasXRate(venueDTO.getRate())));
		imageLoader.DisplayImage(venueDTO.getImagenIco(), imagenIcono);
		return (item);
   }
	
	public int getImagenEstrellasXRate(double rate){
		int imagenEstrellas = R.drawable.estrellas_3_5;
		if(rate>0&&rate<=1){
			imagenEstrellas = R.drawable.estrellas_1;
		}else if(rate>1&&rate<2){
			imagenEstrellas = R.drawable.estrellas_1_5;
		}else if(rate==2){
			imagenEstrellas = R.drawable.estrellas_2;
		}else if(rate>2&&rate<3){
			imagenEstrellas = R.drawable.estrellas_2_5;
		}else if(rate==3){
			imagenEstrellas = R.drawable.estrellas_3;
		}else if(rate>3&&rate<4){
			imagenEstrellas = R.drawable.estrellas_3_5;
		}else if(rate==4){
			imagenEstrellas = R.drawable.estrellas_4;
		}else if(rate>4&&rate<5){
			imagenEstrellas = R.drawable.estrellas_4_5;
		}else if(rate==5){
			imagenEstrellas = R.drawable.estrellas_5;
		}
		return imagenEstrellas;
	}
	
	public int getCount() {
		return datos.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}	
	
	

}
